//
//  MANoteDetailHeader.m
//  meiang_edu
//
//  Created by 李国栋 on 2018/10/17.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MANoteDetailHeader.h"

#import "MACountContentViewController.h"
#import "CollectionHShowView.h"
#import "MANoteImageCell.h"
#import "MANoteDetailViewController.h"

@interface MANoteDetailHeader()


/**
 标题
 */
@property (weak, nonatomic) IBOutlet UILabel *lblTitle;

/**
 发送人
 */
@property (weak, nonatomic) IBOutlet UILabel *lblSender;

/**
 时间
 */
@property (weak, nonatomic) IBOutlet UILabel *lblTime;

/**
 图片容器
 */
@property (weak, nonatomic) IBOutlet UIView *viewImgContent;

/**
 图片展示器,collectionView
 */
@property (weak, nonatomic) CollectionHShowView * viewImageShow;

/**
 统计通知数量
 */
@property (weak, nonatomic) IBOutlet UIView *viewNoticeCount;

/**
 统计情况统计View 高度约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutViewSignHeight;

/**
 图片容器View 高度约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutViewImgsHeight;

/**
 图片容器View 底部约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutViewImgsBottom;


/**
 当前cell数据源
 */
@property (nonatomic ,strong) MANoteManageModel * model;

@end

@implementation MANoteDetailHeader

- (void)setCellDataWithModel:(MANoteManageModel*)model{
    _model = model;
    self.lblTitle.text = model.message_title;
    self.lblSender.text = [NSString stringWithFormat:@"发送人:%@",model.sender_name];
    self.lblContent.text = model.message_content;
    self.lblTime.text = model.send_time;
    NSArray * arrayImgUrl = model.message_image_count.integerValue>0? [model.message_images componentsSeparatedByString:@","]:@[];
    UIImage *placeholderImage = [UIImage imageNamed:@"picture_logo"];
    NSMutableArray * arrayImageModels = [NSMutableArray array];
    for (NSString * url in arrayImgUrl) {
        MANoteImageModel * imgModel = [MANoteImageModel new];
        imgModel.url = url;
        imgModel.placeholderImage = placeholderImage;
        [arrayImageModels addObject:imgModel];
    }
    [_viewImageShow setDataSource:arrayImageModels];
    
    
    NSInteger rowCount = arrayImgUrl.count/3;
    if ((arrayImgUrl.count%3) != 0) {
        rowCount += 1;
    }
    
    //图片区域高度
    CGFloat imageContentHeight = [self getImageSize].height * rowCount + (rowCount>0?(4*(rowCount-1)):0);
    self.layoutViewImgsHeight.constant = imageContentHeight;
    if (model.message_image_count.integerValue>0) {
        self.layoutViewImgsBottom.constant = 12;
    }else{
        self.layoutViewImgsBottom.constant = 0;
    }
    
    //   计算高度
    CGFloat height =  287 - 20-68;//这个高度包括除了内容文字和图片区域的高度之和
    height += [self.lblContent sizeThatFits:CGSizeMake(SCREEN_WIDTH - 12*2 - 4*2, MAXFLOAT)].height;
    
    height += imageContentHeight;
    height -= 66;
    self.layoutViewSignHeight.constant = 0;
    self.viewReNotice.hidden = YES;
    self.viewNoticeCount.hidden = YES;
    
//    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
}

- (void)setDataWithModel:(MANoteManageModel*)model{
    _model = model;
    self.lblTitle.text = model.message_title;
    self.lblSender.text = [NSString stringWithFormat:@"发送人:%@",model.sender_name];
    self.lblContent.text = model.message_content;
    self.lblTime.text = model.send_time;
    NSArray * arrayImgUrl = model.message_image_count.integerValue>0? [model.message_images componentsSeparatedByString:@","]:@[];
    UIImage *placeholderImage = [UIImage imageNamed:@"picture_logo"];
    NSMutableArray * arrayImageModels = [NSMutableArray array];
    for (NSString * url in arrayImgUrl) {
        MANoteImageModel * imgModel = [MANoteImageModel new];
        imgModel.url = url;
        imgModel.placeholderImage = placeholderImage;
        [arrayImageModels addObject:imgModel];
    }
    [_viewImageShow setDataSource:arrayImageModels];
    
    
    NSInteger rowCount = arrayImgUrl.count/3;
    if ((arrayImgUrl.count%3) != 0) {
        rowCount += 1;
    }
    
    //图片区域高度
    CGFloat imageContentHeight = [self getImageSize].height * rowCount + (rowCount>0?(4*(rowCount-1)):0);
    self.layoutViewImgsHeight.constant = imageContentHeight;
    if (model.message_image_count.integerValue>0) {
        self.layoutViewImgsBottom.constant = 12;
    }else{
        self.layoutViewImgsBottom.constant = 0;
    }
    
    //   计算高度
    CGFloat height =  287 - 20-68;//这个高度包括除了内容文字和图片区域的高度之和
    height += [self.lblContent sizeThatFits:CGSizeMake(SCREEN_WIDTH - 12*2 - 4*2, MAXFLOAT)].height;
    
    height += imageContentHeight;
    if ((![model.message_typechild isEqualToString:@"活动"]) &&
        (![model.message_typechild isEqualToString:@"通知"]) ) {
        height -= 66;
        self.layoutViewSignHeight.constant = 0;
        self.viewReNotice.hidden = YES;
        self.viewNoticeCount.hidden = YES;
    }
    
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
}

- (CGFloat)cellHeightWithModel:(MANoteManageModel*)model{
    self.lblContent.text = model.message_content;
    CGFloat height = [self.lblContent   sizeThatFits:CGSizeMake(SCREEN_WIDTH - 12*2, MAXFLOAT)].height;
    return height + 283 - 20;
}

- (CGSize)getImageSize{
    CGFloat imageWidth = (SCREEN_WIDTH - 4*2-12*2-4*2)/3.0;
    CGFloat imageHeight = imageWidth * (68.0/108);
    return CGSizeMake(imageWidth, imageHeight);
}

#pragma mark - init

- (void)awakeFromNib {
    [super awakeFromNib];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = [self getImageSize];
    layout.minimumLineSpacing = 4;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    CollectionHShowView * hView = [[CollectionHShowView alloc] initWithNibCell:@"MANoteImageCell" modelName:@"MANoteImageModel" cellSize:[self getImageSize] layout:layout];
    [_viewImgContent addSubview:hView];
    [hView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.right.bottom.mas_equalTo(0);
    }];
    hView.collectionView.scrollEnabled = NO;
    _viewImageShow = hView;
    _viewImageShow.clipsToBounds = YES;
    
    //添加长按手势
    UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    
    longPressGesture.minimumPressDuration=1.5f;//设置长按 时间
    [self addGestureRecognizer:longPressGesture];
}

#pragma mark - 长按手势
-(void)longPress:(UILongPressGestureRecognizer *)longRecognizer{
    if (longRecognizer.state==UIGestureRecognizerStateBegan) {
        if ([self.delegate respondsToSelector:@selector(noteDetailHeaderDidLongPress:model:)]) {
            [self.delegate noteDetailHeaderDidLongPress:self model:_model];
        }
    }
}

- (IBAction)tapDelete:(id)sender {
    if ([self.delegate respondsToSelector:@selector(noteDetailHeaderDidTapDelete:model:)]) {
        [self.delegate noteDetailHeaderDidTapDelete:self model:self.model];
    }
}

/**
 通知情况统计
 */
- (IBAction)tapNoticeCount:(id)sender {
    MACountContentViewController * vc = [MACountContentViewController new];
    vc.model = _model;
    MANoteDetailViewController * detailVC = (MANoteDetailViewController *)self.viewController;
    vc.funcItemModel = detailVC.funcItemModel;
    vc.countType = MACountContentTypeNormal;
    [self.viewController.navigationController pushViewController:vc animated:YES];
    
}

/**
 重复通知未读用户
 */
- (IBAction)tapReNotice:(id)sender {
    [MANetwork GET:[NSString stringWithFormat:@"%@%@",RENOTICE_UNREAD_USER,_model.keyid] parameters:nil autoShowFailAlert:YES showLoading:YES success:^(id response, BOOL success) {
        if (success) {
            [MBManager showBriefAlert:@"通知成功"];
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
